library hc_toast_util;

import 'dart:math';

import 'package:flutter/material.dart';
import 'package:hc_ui/config/hc_customer_config.dart';
import 'package:hc_ui/theme/hc_toast_theme.dart';
import 'package:hc_ui/utils/hc_color_util.dart';
import 'package:hc_ui/widget/icon_text/hc_icon_text.dart';

import '../constants/hc_size.dart';

part '../widget/toast/hc_toast.dart';

/// 展示位置
/// [top]顶部
/// [center]中间
/// [bottom]底部
enum HcToastPosition { top, center, bottom }

class HcToastUtil {
  const HcToastUtil._();

  //上一次展示的土司
  static _HcToast? _lastHcToast;

  /// 展示土司
  static void showToast({
    required BuildContext context,
    required String text,
    HcToastPosition position = HcToastPosition.center,
    Widget? icon,
    VoidCallback? onDismiss,
    showMask = false,
    Color? textColor,
    Duration? duration,
    HcToastTheme? themeData,
  }) {
    final OverlayState overlayState = Overlay.of(context);
    //上一个如果存在就让他消失
    _lastHcToast?.hide();
    _lastHcToast = null;
    //构建土司弹窗
    final OverlayEntry overlayEntry =
        OverlayEntry(builder: (BuildContext context) {
      return _HcToastContainer(
        text: text,
        textColor: textColor,
        themeData: themeData,
        icon: icon,
        position: position,
        showMask: showMask,
      );
    });
    final _HcToast currentToast =
        _HcToast(overlayState: overlayState, overlayEntry: overlayEntry);
    _lastHcToast = currentToast;
    //如果没有定义则 每分钟阅读300字 每秒5个字 最小显示时长一秒 最大显示时长5秒
    final Duration finalDuration =
        duration ?? Duration(seconds: max(1, min(5, (text.length / 5).ceil())));
    //展示
    currentToast.show(duration: finalDuration, onDismiss: onDismiss);
  }

  //展示成功的土司
  static void showSuccess(
      {required BuildContext context,
      required String text,
      Widget? icon,
      Color? textColor,
      bool showMask = false,
      Color? background,
      VoidCallback? onDismiss,
      Duration? duration}) {
    icon ??= const Icon(
      Icons.check_outlined,
    );
    duration ??= const Duration(seconds: 3);
    return showToast(
        context: context,
        text: text,
        duration: duration,
        showMask: showMask,
        textColor: textColor,
        themeData: HcToastTheme(background: background),
        position: HcToastPosition.center,
        onDismiss: onDismiss,
        icon: icon);
  }

  ///展示失败的土司
  static showFail(
      {required BuildContext context,
      required String text,
      Widget? icon,
      VoidCallback? onDismiss,
      Color? textColor,
      bool showMask = false,
      Color? background,
      Duration? duration}) {
    icon ??= const Icon(
      Icons.close_outlined,
      size: HcSize.largeIconSIze,
    );
    duration ??= const Duration(seconds: 3);
    return showToast(
        context: context,
        text: text,
        textColor: textColor,
        themeData: HcToastTheme(background: background),
        duration: duration,
        onDismiss: onDismiss,
        showMask: showMask,
        position: HcToastPosition.center,
        icon: icon);
  }

  /// 隐藏土司
  static void hideToast({onDismiss}) {
    _lastHcToast?.hide(onDismiss: onDismiss);
    _lastHcToast = null;
  }

  ///展示加载中
  static void showLoading(
      {required BuildContext context,
      required String text,
      Widget? icon,
      bool showMask = true,
      VoidCallback? onDismiss,
      Color? progressColor,
      Duration? duration}) {
    icon ??= SizedBox(
      width: HcSize.largeIconSIze,
      height: HcSize.largeIconSIze,
      child: Center(
        child: CircularProgressIndicator(
          color: progressColor,
        ),
      ),
    );
    duration ??= const Duration(seconds: 3);
    return showToast(
        context: context,
        text: text,
        showMask: showMask,
        duration: duration,
        onDismiss: onDismiss,
        position: HcToastPosition.center,
        icon: icon);
  }

  ///隐藏加载中
  static void hideLoading({onDismiss}) => hideToast(onDismiss: onDismiss);

// static showSnackBar({
//   required BuildContext context,
//   required String text,
// }) {
//   ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(text)));
// }
}
